package com.lovepick;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lovepick.http.HttpThread;
import com.lovepick.util.BaseUtils;
import com.lovepick.util.Constant;

public class ApplyDialogActivity extends Activity
{
	private TextView mGoTimeText;
	private TextView mSrcText;
	private TextView mDestText;
	private TextView mPassengerText;
	private TextView mDriverText;
	private TextView mMoneyText;
	private TextView mCountText;
	private EditText mPayPassword;
	private ProgressBar mBar;

	private int mPasRouteID;
	private int mDriOrderID;
	private int mPasMoney;

	private HttpThread mThread = null;
	private ApplyHandler mHandler = null;


	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_apply);
		findViews();
		mBar.setVisibility(View.GONE);
		Intent data = getIntent();
		if (data != null)
		{
			mGoTimeText.setText(data.getStringExtra("gotime"));
			mPassengerText.setText(data.getStringExtra("passenger"));
			mDriverText.setText(data.getStringExtra("driver"));
			mSrcText.setText(data.getStringExtra("src"));
			mDestText.setText(data.getStringExtra("dest"));
			mCountText.setText(String.valueOf(data.getIntExtra("count", 0)));
			mMoneyText.setText(String.valueOf(data.getIntExtra("money", 0)));

			mPasRouteID = data.getIntExtra("passengerRouteID", 0);
			mDriOrderID = data.getIntExtra("driverOrderID", 0);
			mPasMoney = data.getIntExtra("money", 0);
		}

		mHandler = new ApplyHandler(this);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

	}

	private void findViews()
	{
		mGoTimeText = (TextView) this.findViewById(R.id.apply_gotime);
		mSrcText = (TextView) this.findViewById(R.id.apply_src);
		mDestText = (TextView) this.findViewById(R.id.apply_dest);
		mPassengerText = (TextView) this.findViewById(R.id.apply_passenger);
		mDriverText = (TextView) this.findViewById(R.id.apply_driver);
		mCountText = (TextView) this.findViewById(R.id.apply_count);
		mMoneyText = (TextView) this.findViewById(R.id.apply_money);
		mPayPassword = (EditText) this.findViewById(R.id.pay_password);
		mBar = (ProgressBar) this.findViewById(R.id.apply_progress);
	}

	public void doApply(View v)
	{
		switch (v.getId())
		{
			case R.id.btn_apply_cancel:
				mThread = null;
				setResult(Activity.RESULT_CANCELED);
				finish();
				break;

			case R.id.btn_apply_ok:
				String payPassword = mPayPassword.getText().toString();
				if (payPassword.isEmpty())
				{
					BaseUtils.showToast(getApplicationContext(), "请输入支付密码");
				}
				else
				{
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("passengerRouteID", String.valueOf(mPasRouteID)));
					params.add(new BasicNameValuePair("driverOrderID", String.valueOf(mDriOrderID)));
					params.add(new BasicNameValuePair("passengerMoney", String.valueOf(mPasMoney)));
					params.add(new BasicNameValuePair("payPassword", payPassword));
					mThread = new HttpThread(mHandler, Constant.HTTP_METHOD_POST, Constant.HTTP_URL_ORDER_BUY, params);
					mThread.start();
					mBar.setVisibility(View.VISIBLE);
				}

				break;

			default:
				break;
		}
	}

	private static class ApplyHandler extends Handler
	{
		private WeakReference<ApplyDialogActivity> mReference;
		private ApplyDialogActivity mDialog;

		public ApplyHandler(ApplyDialogActivity dialog)
		{
			mReference = new WeakReference<ApplyDialogActivity>(dialog);
		}

		@Override
		public void handleMessage(Message msg)
		{
			mDialog = mReference.get();
			switch (msg.what)
			{
				case Constant.MSG_HTTP_SUCCESS:
					mDialog.mBar.setVisibility(View.VISIBLE);
					String httpResult = msg.getData().getString(Constant.HTTP_RESULT);
					if (Constant.HTTP_ORDER_ACCEPT_COUNT_NOT_ENOUGH.equals(httpResult))
					{
						BaseUtils.showToast(mDialog, R.string.count_not_enough);
					}
					else if (Constant.HTTP_ORDER_APPLY_SUCCESS.equals(httpResult))
					{
						mDialog.setResult(Activity.RESULT_OK);
					}
					else {
						mDialog.setResult(Activity.RESULT_CANCELED);
					}

					break;

				default:
					mDialog.mBar.setVisibility(View.VISIBLE);
					mDialog.setResult(Activity.RESULT_CANCELED);
					break;
			}

			mDialog.finish();
		}
	}

}
